package cn.tedu.rediscluster;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class RedisClusterApplication {
    @Autowired
    private RedisConnectionFactory f;

    /*
     redis自动配置中会创建两个RedisTemplate实例
     - redisTemplate RedisTemplate
     - stringRedisTemplate StringRedisTemplate
     StringRedisTemplate 存放字符串键值对,如果是复杂的对象,可以转成json存放
     @Qualifier指定

    */
    @Autowired
   // @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;


    public static void main(String[] args) {

        SpringApplication.run(RedisClusterApplication.class, args);
    }
//    spring执行流程
    //扫描-创建实例-依赖注入-@PostConstruct

    @PostConstruct
    public void test(){
        RedisClusterConnection c = f.getClusterConnection();
        for (int i=0;i<100;i++){
            String k="k"+i;
            String v="v"+i;
            c.set(k.getBytes(), v.getBytes());

        }
        byte[] v = c.get("k34".getBytes());
        String s = new String(v);
        System.out.println(s);

        System.out.println("----------------------------------");

        System.out.println(redisTemplate.getClass().getName());
        ValueOperations o = redisTemplate.opsForValue();

        //opsForValue普通键值对数据的操作对象
        o.set("k", "v");
    }

}
